// pages/orderDetail/unpaid.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modal: false,
    order_id: '',
    detail: '',
    cutdown: {
      'hh': 0,
      'dd': 0,
      'mm': 0,
      'ss': 0
    }, //倒计时
    setintvalid: null,
    notcut: false, //是否结束
    heimg: '',
    password: '',
    idx: ''
  },
  // 删除订单
  del_order(e) {
    let that = this;
    wx.showModal({
      title: '',
      content: '是否删除该订单',
      success: function(res) {
        if (res.confirm) {
          wh.Post('/api/mall.order/del', {
            order_id: that.data.order_id
          }, function(res) {
            wx.showToast({
              title: '删除成功',
              icon: 'none',
              duration: 2000,
              complete: function() {
                setTimeout(function() {
                  wx.navigateTo({
                    url: '../order/order?c=-1'
                  })
                }, 2000);
              }
            });
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 提醒发货
  remind(e) {
    let that = this;
    wh.Post('/api/mall.order/remind', {
      order_id: that.data.order_id
    }, function(res) {
      wx.showToast({
        title: '提醒成功',
        icon: 'none',
        duration: 2000,
      });
    })
  },
  // 确认收货
  confirm(e) {
    let that = this;
    wx.showModal({
      title: '',
      content: '是否确认收货/消费',
      success: function(res) {
        if (res.confirm) {
          wh.Post('/api/mall.order/confirm', {
            order_id: that.data.order_id
          }, function(res) {
            wx.showToast({
              title: '收货/消费成功',
              icon: 'none',
              duration: 2000,
              complete: function() {
                setTimeout(function() {
                  wx.navigateTo({
                    url: '../order/order?c=-1'
                  })
                }, 2000);
              }
            });
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 取消订单
  cancels() {
    let that = this;
    wx.showModal({
      title: '',
      content: '是否取消该订单',
      success: function(res) {
        if (res.confirm) {
          wh.Post('	/api/mall.order/cancel', {
            order_id: that.data.order_id
          }, function(res) {
            wx.showToast({
              title: '取消成功',
              icon: 'none',
              duration: 2000,
              complete: function() {
                setTimeout(function() {
                  wx.navigateTo({
                    url: '../order/order?c=-1'
                  })
                }, 2000);
              }
            });
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 输入密码进行订单核销
  password(e) {
    this.setData({
      password: e.detail.value
    })
  },
  write_off_pass() {
    let that = this;
    wh.Post('/api/mall.order/write_off_pass', {
      order_id: that.data.order_id,
      password: that.data.password
    }, function(res) {
      wx.showToast({
        title: '核销成功',
        icon: 'none',
        duration: 2000,
        complete: function() {
          setTimeout(function() {
            wx.navigateTo({
              url: '../order/order?c=-1'
            })
          }, 2000);
        }
      });
    })
  },

  // 订单核销二维码
  cancel() {
    let that = this;
    wx.getExtConfig({
      success: function(res) {
        let heimg = res.extConfig.apiurl + '/api/mall.order/write_off_qrcode?order_id=' + that.data.order_id;
        that.setData({
          modal: true,
          heimg: heimg
        })
      }
    });
  },
  close() {
    this.setData({
      modal: false
    })
  },
  // 倒计时
  end_bargain: function(end_time, thistime) {
    let cha = end_time - thistime;
    let that = this;
    that.setintvalid = setInterval(function() {
      cha -= 1000;
      if (cha <= 0) {
        clearInterval(that.setintvalid);
        cha = 0;
        that.setData({
          notcut: true
        });
        return;
      }
      that.time_meter(cha)
    }, 1000);
  },
  //时间计算
  time_meter: function(timer) {
    let that = this;
    let dd = parseInt(timer / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
    let hh = parseInt(timer / 1000 / 60 / 60 % 24, 10); //计算剩余的小时数
    let mm = parseInt(timer / 1000 / 60 % 60, 10); //计算剩余的分钟数
    let ss = parseInt(timer / 1000 % 60, 10); //计算剩余的秒数
    mm = that.checkTime(mm);
    ss = that.checkTime(ss);
    let cutdown = {
      'dd': dd,
      'hh': hh,
      'mm': mm,
      'ss': ss
    };
    that.setData({
      cutdown: cutdown
    })
  },
  checkTime: function(i) {
    if (i < 10) {
      i = "0" + i;
    }
    return i;
  },

  // 订单详情
  item_detail(order_id) {
    let that = this;
    wh.Post('/api/mall.order/item_detail', {
      order_id: order_id
    }, function(res) {
      console.log(res)
      if (res.count_time) {
        clearInterval(that.setintvalid); //清除倒计时
        let thistime = new Date().getTime(); //判断时间
        console.log(thistime)
        let end_time = res.count_time + '000'; //精确到毫秒数 
        console.log(end_time)
        if (thistime < end_time) {
          that.end_bargain(end_time, thistime);
        } else {
          that.setData({
            notcut: true
          }); //活动结束
        }
      }
      that.setData({
        detail: res
      })
    })
  },
  // 开始支付及支付后的回调
  startPay() {
    let that = this;
    wh.Post('/api/mall.order/payment', {
      order_id: that.data.detail.order_id
    }, function (res) {
      console.log('支付信息', res)
      // 微信支付
      wx.requestPayment({
        'timeStamp': res.timeStamp + '',
        'nonceStr': res.nonceStr,
        'package': res.package,
        'signType': res.signType,
        'paySign': res.paySign,
        'success': function (ref) {
          console.log('支付成功', ref)
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 2000,
            complete: function () {
              wx.reLaunch({
                url: '../paysuss/paysuss',
              })
            }
          })
        },
      })
    })
  },
  // 跳转至商品详情
  toGoods(e) {
    let that = this,
      id = e.currentTarget.dataset.id,
      act_type = e.currentTarget.dataset.act_type;
    if (act_type == 1) {
      wx.navigateTo({
        url: '../details/details?goods_id=' + id,
      })
    } else if (act_type == 2) {
      wx.navigateTo({
        url: '../cutDetails/cutDetails?bargainId=' + id,
      })
    } else if (act_type == 3) {
      wx.navigateTo({
        url: '../killDetails/killDetails?seckill_id=' + id,
      })
    } else if (act_type == 4) {
      wx.navigateTo({
        url: '../groupDetails/groupDetails?group_id=' + id,
      })
    }
  }, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options.order_id)
    this.item_detail(options.order_id)
    this.setData({
      order_id: options.order_id,
    })
    if (options.idx) {
      this.setData({
        idx: options.idx
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    if (this.data.idx == 1) {
      wx.switchTab({
        url: '../index/index',
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})